package com.compomics.prioritizer.general;

import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: Niklaas Colaert
 * Date: 7-nov-2008
 * Time: 8:09:44
 * To change this template use File | Settings | File Templates.
 */
public class TransformMascotModFile {



    public TransformMascotModFile(String aModFileLocation){

//replace all * in the mod_file by !!!!!!!!!!\\n

        
        String  mod_file= this.getModFile(aModFileLocation);
        String[] modifications = mod_file.split("!!!!!!!!\n");

        String result = "insert into `modification`(name, mono_isotopic, average, username, creationdate, modificationdate) values ";
        for(int i = 0; i<modifications.length; i++){
            if(modifications[i].indexOf("Hidden") == -1){
                String[] mod_ele = modifications[i].split("\n");
                String title = mod_ele[0].substring(6);
                String[] massMod = mod_ele[1].split(" ");
                if(massMod.length == 2){
                    String[] mod = massMod[0].split(":");
                    massMod[0] =  mod[1];
                    result = result + " ('"+title+"',"+massMod[0] + "," + massMod[1] +",user(), now(),now()),\n";
                } else {
                    result = result + " ('"+title+"',"+massMod[1] + "," + massMod[2] +",user(), now(),now()),\n";
                }



            }
        }
        System.out.println(result);

    }

    public String getModFile(String aModFileLocation)  {
        String modFile = "";
        BufferedReader input = null;
        try {
            input = new BufferedReader(new FileReader(aModFileLocation));

                    String line = null; //not declared within while loop
                    /*
                    * readLine is a bit quirky :
                    * it returns the content of a line MINUS the newline.
                    * it returns null only for the END of the stream.
                    * it returns an empty String if two newlines appear in a row.
                    */
                    while (( line = input.readLine()) != null){
                      modFile = modFile + "\n" + line;
                    }
            } catch (FileNotFoundException e) {
              e.printStackTrace();
            } catch (IOException e) {
              e.printStackTrace();
            }
       return modFile;
    }

    public static void main(String[] args){
        TransformMascotModFile trans = new TransformMascotModFile("E:\\mod_file");
    }
}
